<?php
namespace app\index\model;
use think\Model;
use caretree\Caretree;
class Goods extends Model
{
    public function getRecposGoods($recposId,$limit=''){
		//热卖商品
		$_hotIds = db('rec_item')->where(array('value_type'=>1,'recpos_id'=>$recposId))->select();
		
		$hotIds = array();
		foreach($_hotIds as $k =>$v){
				   $hotIds[] = $v['value_id'];
		}
		$map['id']=array('IN',$hotIds);
		$recRes = $this->field('id,mid_thumb,goods_name,shop_price,markte_price')->where($map)->limit($limit)->select();
		return $recRes;
	}
	//获取首页一二级分类下的所有推荐商品
	public function getIndexRecposGoods($cateId,$recposId){
		
		$cateTree = new CareTree();
		$sonIds = $cateTree->childrenids($cateId,db('category'));
		$sonIds[]=$cateId;
		//2,获取新品推荐位里符合条件的商品信息
		$_recGoods = db('rec_item')->where(array('value_type'=>1,'recpos_id'=>$recposId))->select();
		$recGoods=array();
		foreach($_recGoods as $kk =>$vv){
			$recGoods[]=$vv['value_id'];
		}
		 $map['id'] = array('IN',$recGoods);
		 $map['category_id']=array('IN',$sonIds);
		 // dump($map);
		 $goodRes = db('goods')->where($map)->limit(6)->order('id DESC')->select();
		return $goodRes;
	}
	
	//获取商品会员价格
	public function getMemberPrice($goods_id){
		$levelId = session('level_id');
		$levelRate = session('level_rate');
		$goodsInfo = $this->find($goods_id);
		if(session('level_rate')){
			$memberPrice = db('member_price')->where(array('mlevel_id'=>$levelId,'goods_id'=>$goods_id))->find();
			if($memberPrice){
				$price = $memberPrice['mprice'];
			}else{
				$levelRate=$levelRate/100;
				$price=$goodsInfo['shop_price']*$levelRate;
			}
		}else {
			$price = $goodsInfo['shop_price'];
		}
		return $price;// 最终会员价格
	}
	public function getShopPrice($goods_id){
		$goodsInfo = $this->field('shop_price')->find($goods_id);
		return $goodsInfo['shop_price'];
	}
}
